//  this library.
import 'package:flutter/material.dart';
import 'package:hktk_app/dao/practice_dao.dart';
import 'package:hktk_app/exception/dao_exception.dart';
import 'package:hktk_app/ienum/practice/ct_enum.dart';
import 'package:hktk_app/ienum/practice/one_do_source_enum.dart';
import 'package:hktk_app/ienum/question/question_type_enum.dart';
import 'package:hktk_app/model/practice/practice_model.dart';
import 'package:hktk_app/model/practice/practice_question_model.dart';
import 'package:hktk_app/model/question/paper_data_model.dart';
import 'package:hktk_app/model/question/question_data_model.dart';
import 'package:hktk_app/pages/question/child/question_exam_page.dart';
import 'package:hktk_app/util/data_util.dart';
import 'package:hktk_app/util/my_util.dart';

/// 每日一练记录试题显示
class RecordQuestionPage extends StatefulWidget {
  /// 每日一练试题相关信息
  final PracticeModel practiceModel;

  const RecordQuestionPage({
    super.key,
    required this.practiceModel,
  });

  @override
  State<RecordQuestionPage> createState() => _RecordQuestionPageState();
}

class _RecordQuestionPageState extends State<RecordQuestionPage> {
  @override
  Widget build(BuildContext context) {
    debugPrint("recordQuestionPage 页面 -> build 方法执行 ===> ");
    return QuestionExamPage(
      paperInit: _initData,
      quesInit: _quesInit,
      type: QuestionTypeEnum.dailyRecord,
    );
  }

  Future<Map<String, dynamic>> _initData() async {
    Map<String, dynamic> map = {};
    try {
      PaperDataModel model = _getPaperData(widget.practiceModel);
      map["data"] = model;
      map["suc"] = true;
    } catch (e) {
      debugPrint("recordQuestionPage 页面 -> 获取试卷信息方法发生异常 ===> $e");
      map = {"suc": false, "msg": "未知错误，请重试"};
      if (e is DaoException) {
        debugPrint("recordQuestionPage 页面 -> dao异常 ===> ${e.msg}");
        map["msg"] = e.msg;
      }
    }
    return map;
  }

  /// 计算试题信息相关model
  PaperDataModel _getPaperData(PracticeModel model) {
    List<DtkInfoModel> dtkList = [];
    List<BogInfoModel> bogList = [];

    int index = 0;
    List<ContentPractice> list = model.content!;
    for (ContentPractice con in list) {
      List<QsPractice> list2 = con.qs!;
      if (list2.isNotEmpty) {
        bogList.add(BogInfoModel(
          name: con.tyName!,
          type: MyUtil.getTypeEnum(con.tyName!),
          total: list2.length,
          dtkList: [],
        ));
        for (QsPractice qs in list2) {
          dtkList.add(DtkInfoModel(
            index: index,
            id: qs.id!,
            type: MyUtil.getTypeEnum(con.tyName!),
            sign: '',
            userAns: [],
          ));
          index++;
        }
      }
    }

    // 每日一练每次都是重第一题开始
    return PaperDataModel(
      name: model.name!,
      id: model.id!,
      collectId: model.id!,
      lastIndex: 0,
      dtkList: dtkList,
      bogDtkList: bogList,
      rType: CtEnum.everyday,
      startDate: 0,
    );
  }

  /// 加载试题数据方法
  Future<Map<String, dynamic>> _quesInit(String qId, String cid) async {
    Map<String, dynamic> map = {};
    try {
      PracticeQuestionModel model = await PracticeDao.clientOneDo(
        id: qId,
        oneDoSourceEnum: OneDoSourceEnum.erveryDay,
        industryCode: dataUtilComm.dataModel.industryId,
        examId: dataUtilComm.dataModel.examId,
        categoryId: dataUtilComm.dataModel.categoryId,
        ctEnum: CtEnum.everyday,
        view: true,
        cid: cid,
      );
      QuestionDataModel dataModel = MyUtil.getQuestionData(model);
      map["suc"] = true;
      map["data"] = dataModel;
    } catch (e) {
      debugPrint("recordQuestionPage 页面 -> 加载试题数据方法发生异常 ===> $e");
      map = {"suc": false, "msg": "未知错误，请重试"};
      if (e is DaoException) {
        debugPrint("recordQuestionPage 页面 -> dao异常 ===> ${e.msg}");
        map["msg"] = e.msg;
      }
    }

    return map;
  }
}
